package com.gitee.zycra.union.api.service;

import com.gitee.zycra.union.api.common.bo.CommonResult;
import com.gitee.zycra.union.api.common.vo.InterfaceFolderVO;
import com.gitee.zycra.union.api.manager.ApiInterfaceFolderManager;
import com.gitee.zycra.union.api.manager.ApiInterfaceManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zycra
 */
@Slf4j
@Service
public class ApiInterfaceFolderService {

    @Autowired
    private ApiInterfaceFolderManager apiInterfaceFolderManager;

    @Autowired
    private ApiInterfaceManager apiInterfaceManager;

    /**
     * 添加目录
     *
     * @param projectId  项目ID
     * @param folderName 目录名称
     * @return 添加结果
     */
    public CommonResult<String> addFolder(Long projectId, String folderName) {
        apiInterfaceFolderManager.addFolder(projectId, folderName);
        return CommonResult.success();
    }

    /**
     * 更新目录
     *
     * @param folderId   目录ID
     * @param folderName 目录名称
     * @return 添加结果
     */
    public CommonResult<String> updateFolder(Long folderId, String folderName) {
        apiInterfaceFolderManager.updateFolder(folderId, folderName);
        return CommonResult.success();
    }

    /**
     * 删除目录
     *
     * @param folderId 目录ID
     * @return 添加结果
     */
    public CommonResult<String> deleteFolder(Long folderId) {
        int interfaceAmount = apiInterfaceManager.countByFolderId(folderId);
        if (interfaceAmount > 0) {
            return CommonResult.failed("目录不为空");
        }
        apiInterfaceFolderManager.deleteFolder(folderId);
        return CommonResult.success();
    }

    /**
     * 根据项目ID查询目录
     *
     * @param projectId 项目ID
     * @return 目录集合
     */
    public CommonResult<List<InterfaceFolderVO>> selectByProjectId(Long projectId) {
        return CommonResult.success(apiInterfaceFolderManager.selectByProjectId(projectId));
    }
}
